#include "Toolbar.h"


#include <QIcon>
#include "LabelButton.h"

#define BUTTON_SIZE 40
#define CURRENT_SIZE 85

#include <QMatrix>

Toolbar::Toolbar(QWidget* parent)
	: QWidget(parent)
{
	layout = new QGridLayout(this);
	this->setLayout( layout );


	m_pixmapCurrent = QPixmap("resources/images/scene/vloer.png");
	m_stringCurrent = QString("VV");

	m_selected = new QLabel();
	m_selected->setPixmap(QPixmap("resources/images/scene/vloer.png").scaled(CURRENT_SIZE, CURRENT_SIZE));
	layout->addWidget(m_selected, 0, 0, 2, 2);
	
	LabelButton* button = new LabelButton(QPixmap("resources/images/scene/vloer.png").scaled(BUTTON_SIZE, BUTTON_SIZE), this);
	layout->addWidget(button, 2, 0, 1, 1);
    QObject::connect(button, SIGNAL(clicked()), this, SLOT(selectVloer()));
	button =  new LabelButton(QPixmap("resources/images/scene/muur.png").scaled(BUTTON_SIZE, BUTTON_SIZE), this);
	layout->addWidget(button, 3, 0, 1, 1);
    QObject::connect(button, SIGNAL(clicked()), this, SLOT(selectMuur()));
	button = new LabelButton(QPixmap("resources/images/scene/laser.png").scaled(BUTTON_SIZE, BUTTON_SIZE), this);
	layout->addWidget(button, 4, 0, 1, 1);
    QObject::connect(button, SIGNAL(clicked()), this, SLOT(selectLaser()));
	button = new LabelButton(QPixmap("resources/images/scene/drain.png").scaled(BUTTON_SIZE, BUTTON_SIZE), this);
	layout->addWidget(button, 5, 0, 1, 1);
    QObject::connect(button, SIGNAL(clicked()), this, SLOT(selectDrain()));
	button = new LabelButton(QPixmap("resources/images/scene/afgrond.png").scaled(BUTTON_SIZE, BUTTON_SIZE), this);	
	layout->addWidget(button, 6, 0, 1, 1);
    QObject::connect(button, SIGNAL(clicked()), this, SLOT(selectAfgrond()));
	button = new LabelButton(QPixmap("resources/images/scene/reparatievak.png").scaled(BUTTON_SIZE, BUTTON_SIZE), this);
	layout->addWidget(button, 7, 0, 1, 1);
    QObject::connect(button, SIGNAL(clicked()), this, SLOT(selectReparatievak()));
	button = new LabelButton(QPixmap("resources/images/scene/checkpoint1.png").scaled(BUTTON_SIZE, BUTTON_SIZE), this);
	layout->addWidget(button, 8, 0, 1, 1);
    QObject::connect(button, SIGNAL(clicked()), this, SLOT(selectCheckpoint()));
	button = new LabelButton(QPixmap("resources/images/scene/startpunt.png").scaled(BUTTON_SIZE, BUTTON_SIZE), this);
	layout->addWidget(button, 9, 0, 1, 1);
    QObject::connect(button, SIGNAL(clicked()), this, SLOT(selectStartpunt()));

	// checkpoints
	QMatrix rotation;
	check[0] = new LabelButton(QPixmap("resources/images/scene/checkpoint1.png").scaled(BUTTON_SIZE, BUTTON_SIZE), this);
	layout->addWidget(check[0], 7, 1, 1, 1);
    QObject::connect(check[0], SIGNAL(clicked()), this, SLOT(selectCheckpoint()));
	check[1] = new LabelButton(QPixmap("resources/images/scene/checkpoint2.png").scaled(BUTTON_SIZE, BUTTON_SIZE), this);
	layout->addWidget(check[1], 8, 1, 1, 1);
    QObject::connect(check[1], SIGNAL(clicked()), this, SLOT(selectCheckpoint2()));
	check[2] = new LabelButton(QPixmap("resources/images/scene/checkpoint3.png").scaled(BUTTON_SIZE, BUTTON_SIZE), this);
	layout->addWidget(check[2], 9, 1, 1, 1);
    QObject::connect(check[2], SIGNAL(clicked()), this, SLOT(selectCheckpoint3()));
	hideCheckpoints();
	// lasers

	for(int i = 0; i < 4; i++)
	{
		laser[i] = new LabelButton(QPixmap("resources/images/scene/laser.png").scaled(BUTTON_SIZE, BUTTON_SIZE).transformed(rotation), this);
		layout->addWidget(laser[i], 3 + i, 1, 1, 1);
		rotation.rotate(90);
	}
    QObject::connect(laser[0], SIGNAL(clicked()), this, SLOT(selectLaser()));
    QObject::connect(laser[1], SIGNAL(clicked()), this, SLOT(selectLaserRot90()));
    QObject::connect(laser[2], SIGNAL(clicked()), this, SLOT(selectLaserRot180()));
    QObject::connect(laser[3], SIGNAL(clicked()), this, SLOT(selectLaserRot270()));
	hideLasers();
}

void Toolbar::selectVloer()
{
	m_pixmapCurrent = QPixmap("resources/images/scene/vloer.png");
	m_stringCurrent = QString("VV");
	hideCheckpoints();
	hideLasers();
	m_selected->setPixmap(QPixmap("resources/images/scene/vloer.png").scaled(CURRENT_SIZE, CURRENT_SIZE));
}
void Toolbar::selectMuur()
{
	m_pixmapCurrent = QPixmap("resources/images/scene/muur.png");
	m_stringCurrent = QString("MM");
	hideCheckpoints();
	hideLasers();
	m_selected->setPixmap(QPixmap("resources/images/scene/muur.png").scaled(CURRENT_SIZE, CURRENT_SIZE));
}

void Toolbar::selectLaser()
{
	m_pixmapCurrent = QPixmap("resources/images/scene/laser.png");
	m_stringCurrent = QString("L0");
	hideCheckpoints();
	showLasers();
	m_selected->setPixmap(QPixmap("resources/images/scene/laser.png").scaled(CURRENT_SIZE, CURRENT_SIZE));
}
void Toolbar::selectAfgrond()
{
	m_pixmapCurrent = QPixmap("resources/images/scene/afgrond.png");
	m_stringCurrent = QString("AA");
	hideCheckpoints();
	hideLasers();
	m_selected->setPixmap(QPixmap("resources/images/scene/afgrond.png").scaled(CURRENT_SIZE, CURRENT_SIZE));
}

void Toolbar::selectDrain()
{
	m_pixmapCurrent = QPixmap("resources/images/scene/drain.png");
	m_stringCurrent = QString("DD");

	hideCheckpoints();
	hideLasers();
	m_selected->setPixmap(QPixmap("resources/images/scene/drain.png").scaled(CURRENT_SIZE, CURRENT_SIZE));
}

void Toolbar::selectReparatievak()
{
	m_pixmapCurrent = QPixmap("resources/images/scene/reparatievak.png");
	m_stringCurrent = QString("RR");
	hideCheckpoints();
	hideLasers();
	m_selected->setPixmap(QPixmap("resources/images/scene/reparatievak.png").scaled(CURRENT_SIZE, CURRENT_SIZE));
}
void Toolbar::selectStartpunt()
{
	m_pixmapCurrent = QPixmap("resources/images/scene/startpunt.png");
	m_stringCurrent = QString("SS");
	hideCheckpoints();
	hideLasers();
	m_selected->setPixmap(QPixmap("resources/images/scene/startpunt.png").scaled(CURRENT_SIZE, CURRENT_SIZE));
}
void Toolbar::selectCheckpoint()
{
	m_pixmapCurrent = QPixmap("resources/images/scene/checkpoint1.png");
	m_stringCurrent = QString("C1");
	hideLasers();
	m_selected->setPixmap(QPixmap("resources/images/scene/checkpoint1.png").scaled(CURRENT_SIZE, CURRENT_SIZE));
	showCheckpoints();

}


void Toolbar::selectCheckpoint2()
{
	m_pixmapCurrent = QPixmap("resources/images/scene/checkpoint2.png");
	m_stringCurrent = QString("C2");
	m_selected->setPixmap(QPixmap("resources/images/scene/checkpoint2.png").scaled(CURRENT_SIZE, CURRENT_SIZE));
}
void Toolbar::selectCheckpoint3()
{
	m_pixmapCurrent = QPixmap("resources/images/scene/checkpoint3.png");
	m_stringCurrent = QString("C3");
	m_selected->setPixmap(QPixmap("resources/images/scene/checkpoint3.png").scaled(CURRENT_SIZE, CURRENT_SIZE));
}

void Toolbar::selectLaserRot90()
{
	QMatrix rotation;
	rotation.rotate(90);
	m_pixmapCurrent = QPixmap("resources/images/scene/laser.png").transformed(rotation);
	m_stringCurrent = QString("L1");
	m_selected->setPixmap(QPixmap("resources/images/scene/laser.png").scaled(CURRENT_SIZE, CURRENT_SIZE).transformed(rotation));
}
void Toolbar::selectLaserRot180()
{
	QMatrix rotation;
	rotation.rotate(180);
	m_pixmapCurrent = QPixmap("resources/images/scene/laser.png").transformed(rotation);
	m_stringCurrent = QString("L2");
	m_selected->setPixmap(QPixmap("resources/images/scene/laser.png").scaled(CURRENT_SIZE, CURRENT_SIZE).transformed(rotation));
}
void Toolbar::selectLaserRot270()
{
	QMatrix rotation;
	rotation.rotate(270);
	m_pixmapCurrent = QPixmap("resources/images/scene/laser.png").transformed(rotation);
	m_stringCurrent = QString("L3");
	m_selected->setPixmap(QPixmap("resources/images/scene/laser.png").scaled(CURRENT_SIZE, CURRENT_SIZE).transformed(rotation));
}

void Toolbar::hideCheckpoints()
{
	for(int i = 0; i < 3; i++)
		check[i]->hide();
}
void Toolbar::showCheckpoints()
{
	for(int i = 0; i < 3; i++)
		check[i]->show();
}
void Toolbar::hideLasers()
{
	for(int i = 0; i < 4; i++) 
		laser[i]->hide();
}
void Toolbar::showLasers()
{
	for(int i = 0; i < 4; i++)
		laser[i]->show();
}

void Toolbar::select(const QString& vakje)
{
	if( vakje == "VV" )
		selectVloer();
	else if( vakje == "L0" )
		selectLaser();
	else if( vakje == "L1" )
		selectLaserRot90();
	else if( vakje == "L2" )
		selectLaserRot180();
	else if( vakje == "L3" )
		selectLaserRot270();
	else if( vakje == "MM" )
		selectMuur();
	else if( vakje == "AA" )
		selectAfgrond();
	else if( vakje == "DD" )
		selectDrain();
	else if( vakje == "C1" )
		selectCheckpoint();
	else if( vakje == "C2" )
		selectCheckpoint2();
	else if( vakje == "C3" )
		selectCheckpoint3();
	else if( vakje == "RR" )
		selectReparatievak();
	else if( vakje.at(0) == 'S')
		selectStartpunt();
	else
		selectVloer();
}
